package com.york.security.config;

import com.york.security.filter.TokenAuthenticationFilter;
import com.york.security.filter.TokenLoginFilter;
import com.york.security.security.DefaultPasswordEncoder;
import com.york.security.security.JwtUtils;
import com.york.security.security.TokenLogoutHandler;
import com.york.security.security.UnauthEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @Description:核心配置类
 * @Data: 2022/3/22 16:13
 * @Author: yangboxiong
 * @Version
 * @since JDK 1.8
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {
    //自定义查询数据库用户名密码和权限信息
    private UserDetailsService userDetailsService;
    //token 管理工具类（token）
    private JwtUtils jwtUtils;
    //密码管理工具
    private DefaultPasswordEncoder defaultPasswordEncoder;
    //redis 操作工具类
    private RedisTemplate redisTemplate;

    @Autowired
    public TokenWebSecurityConfig(UserDetailsService userDetailsService, JwtUtils jwtUtils, RedisTemplate redisTemplate, DefaultPasswordEncoder defaultPasswordEncoder) {
        this.userDetailsService = userDetailsService;
        this.defaultPasswordEncoder = defaultPasswordEncoder;
        this.jwtUtils = jwtUtils;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 设置退出的地址和token，redis操作地址
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                //没有权限访问
                .authenticationEntryPoint(new UnauthEntryPoint())
                //关闭csrf防护
                .and().csrf().disable()
                //授权
                .authorizeRequests()
                //所有请求都必须认证（登录）才能访问，必须放最后
                .anyRequest().authenticated()
                //退出路径
                .and()
                //开启退出功能
                .logout()
                //自定义注销功能的URL地址
                .logoutUrl("/admin/acl/index/logout")
                //添加注销处理程序
                .addLogoutHandler(new TokenLogoutHandler(jwtUtils, redisTemplate)).and()
                //添加过滤器
                .addFilter(new TokenLoginFilter(authenticationManager(), jwtUtils, redisTemplate))
                .addFilter(new TokenAuthenticationFilter(authenticationManager(), jwtUtils, redisTemplate)).httpBasic();
    }

    /**
     * 调用userDetailsService和密码处理
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //自定义查询数据库用户名密码和权限信息
        auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordEncoder);
    }

    /**
     * 配置哪些请求不拦截
     *
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        //以下页面不需要被认证，直接放行
        web.ignoring().antMatchers("/api/**");
    }
}
